package com.lyn.advance.fragments;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.lyn.advance.R;
import com.lyn.advance.interfaces.Advance;
import com.lyn.advance.views.CustomVideo;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * 视频fragment
 * @author longyn 2023/6/30
 * @version 1.0.0
 */
public class VideoFragment extends Fragment implements Advance {

    private static final String TAG = "VideoFragment";

    private final Handler handler = new Handler(Looper.getMainLooper());

    private CustomVideo video;

    /**
     * 包括视频文件路径 和网络url
     */
    private String url;

    private final List<Advance.AdvanceListener> advanceListenerList = new ArrayList<>();

    @Override
    public void init(String path) {
        this.url = path;
    }

    @Override
    public void setType(Type type) {

    }

    @Override
    public void setAdvanceListener(AdvanceListener advanceListener) {
        this.advanceListenerList.add(advanceListener);
    }

    @Override
    public void removeAdvanceListener(AdvanceListener advanceListener) {
        for (AdvanceListener next : advanceListenerList) {
            if (next.equals(advanceListener)) {
                advanceListenerList.remove(next);
            }
        }
    }

    @Override
    public void setIntervalTime(long intervalTime) {

    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_video, container, false);
        video = view.findViewById(R.id.video);
        video.setOnCompletionListener(mp -> {
            Log.d(TAG,"onCompletion");
            handler.post(() -> {
                if (isDetached())return;
                for (AdvanceListener advanceListener:advanceListenerList){
                    advanceListener.onCompletion(Type.VIDEO);
                }
            });
        });
        video.setOnErrorListener((mp, what, extra) -> {
            Log.d(TAG,"onError what:"+what+" extra:"+extra);
            try {
                Field mOnCompletionListenerField = video.getClass().getSuperclass().getDeclaredField("mOnCompletionListener");
                mOnCompletionListenerField.setAccessible(true);
                MediaPlayer.OnCompletionListener mOnCompletionListener = (MediaPlayer.OnCompletionListener) mOnCompletionListenerField.get(video);
                if (mOnCompletionListener != null) {
                    mOnCompletionListener.onCompletion(mp);
                }
            }catch (Throwable e){
                e.printStackTrace();
            }
            return true;
        });
        Log.d(TAG,"onCreateView");
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d(TAG,"onDestroyView");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d(TAG,"onPause");
        if (video!=null){
            video.stopPlayback();
            video.setVisibility(View.GONE);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG,"onResume");
        if (video!=null){
            video.setVisibility(View.VISIBLE);
            if (video.isPlaying()){
                video.pause();
            }
            Log.d(TAG,"播放url:"+url);
            video.setVideoPath(url);
            video.start();
        }
    }
}
